package com.scalar.chiptrack.operations.inventoryps.actioncommands;

import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.operations.inventoryps.forms.InventoryPackingSlipForm;
import com.scalar.chiptrack.operations.inventoryps.dao.InventoryPSDao;
import com.scalar.chiptrack.operations.dataentry.LotDetails;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.commons.SessionKeys;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: munugoti
 * Date: 3 Mar, 2010
 * Time: 7:04:06 PM
 */
public class InventoryPackingSlipModifyCommand extends ActionCommand
{
    private static final String ACTION_NAME = "/inventoryps.do" ;

	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
	{

        InventoryPackingSlipForm inventoryPSForm = (InventoryPackingSlipForm) form;
    	String forwardMapping = "generateps";
        HashMap<String, String> errors = new HashMap<String, String>();
        HashMap<String, String> info = new HashMap<String, String>();
        String action = request.getParameter("action");
        int packingSlipId;
        request.setAttribute(INFO_MESSAGES, info);
        request.setAttribute(ERROR_MESSAGES, errors);

        HttpSession mSession = request.getSession();
        User user = (User) mSession.getAttribute(SessionKeys.USER_KEY);

        if (user == null)
        {
            mSession.setAttribute(SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME);
            return (mapping.findForward("login"));
        }
        else
        {
            if (user.isActionAllowed(Functionality.DATA_ENTRY, User.USER_ACTION_MODIFY))
            {
                request.setAttribute("showMessage", "FALSE");
            }
            else
            {
                request.setAttribute("showMessage", "TRUE");
            }
        }
        try
        {
               packingSlipId = Integer.parseInt( request.getParameter( "packingSlipId") );
        }
        catch (NumberFormatException nfe)
        {
            packingSlipId = -1;
        }
        inventoryPSForm.setPackingSlipId( packingSlipId );

        if ( inventoryPSForm.getPackingSlipId() > 0 )
        {
            InventoryPSDao.loadInventoryPackingSlip( inventoryPSForm );
            inventoryPSForm.setSelectedLotsList( inventoryPSForm.getAvailableLotsList() );

            if( showWaferIds( inventoryPSForm.getSelectedLotsList() ) )
            {
                request.setAttribute( "showWaferIds", "TRUE" );
            }
        }
        else
        {
            if ( inventoryPSForm.getBaseprocessType().equalsIgnoreCase( LOCATION_TYPE_FAB ) || inventoryPSForm.getBaseprocessType().equalsIgnoreCase( LOCATION_TYPE_WAFER_STORE )
               || inventoryPSForm.getBaseprocessType().equalsIgnoreCase( LOCATION_TYPE_SORT ) || inventoryPSForm.getBaseprocessType().equalsIgnoreCase( LOCATION_TYPE_DIE_BANK )
                || "Back Grind".equalsIgnoreCase(inventoryPSForm.getProcessType() ) )
            {
                request.setAttribute( "showWaferIds", "TRUE" );
            }
            else
            {
                request.setAttribute( "showWaferIds", "FALSE" );
            }
            ArrayList<LotDetails> selectedList = new ArrayList<LotDetails>();
            LotDetails lotDetails = null;
            for(int i=0; i < inventoryPSForm.getSelectedLotsList().size(); i++ )
            {
                 lotDetails =   inventoryPSForm.getSelectedLotsList().get( i );
                 lotDetails.setChecked( true );
                 selectedList.add(lotDetails);

            }
            if (selectedList != null && selectedList.size() > 0 )
            {
                inventoryPSForm.setSelectedLotsList( selectedList );
            }
        }

        request.setAttribute( "action", action );

		return mapping.findForward( forwardMapping );
	}
    private boolean showWaferIds(ArrayList<LotDetails> availableLotList)
	{
		boolean showWaferIds = false;
		if( availableLotList == null || availableLotList.size() == 0 )
		{
            showWaferIds = false;
        }
		else
		{
			for( LotDetails	lotDetail:availableLotList )
			{
				if( lotDetail.getWaferIds() != null && lotDetail.getWaferIds().length() > 0 )
				{
					showWaferIds = true;
					break;
				}
			}
		}
		return showWaferIds;
	}
}
